package com.zhuannimei.utility;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.zhuannimei.R;
import com.zhuannimei.adapter.ListPopAdapter;
import com.zhuannimei.view.GrivatyToast;
import com.zhuannimei.view.YYMemuItem;

public class UiUtil {
	/** 下拉选择. */
	public static void showToast(String msg) {
		GrivatyToast.show(msg);
	}
	
	/**
	 * 设置下拉菜单
	 * @param view  哪一个按钮 按下后回弹出下拉菜单
	 * @param list  list.add("上海"),list.add("北京"),list.add("广州")  填充菜单的数据
	 * @param id    参数1 的背景图片id    
	 * @param listener  下拉菜单当点击监听事件
	 * @param context  上下文
	 * @param titlebar 标题栏  用于计算与弹出框当间距
	 */
	public static void setPullDownMenu(View view,List<YYMemuItem> list,int id,
			OnItemClickListener listener,Context context,View titlebar) {
		 LayoutInflater layoutInflater = LayoutInflater.from(context);
		 view.setBackgroundResource(id);
		 View popView = layoutInflater.inflate(R.layout.list_pop, null);
		 ListView popListView = (ListView) popView.findViewById(R.id.pop_list);
		 ListPopAdapter mListPopAdapter = new ListPopAdapter(context, list,R.layout.list_pop_item);
		 popListView.setAdapter(mListPopAdapter);
		 popListView.setOnItemClickListener(listener);
		 setTitleTextDropDown(popView,view,titlebar);
		
	}
	
	/**
	 * 描述：下拉菜单的的实现方法
	 * @param parent  点击的view
	 * @param view    要显示的View
	 * @param offsetMode 不填满的模式
	 * @param titlebar title栏的视图，用于计算间距
	 */
	public static void showWindow(View parent,View view,boolean offsetMode,View titlebar) {
		measureView(view);
		PopupWindow popupWindow;
		int popWidth = parent.getMeasuredWidth();
		int popMargin = (titlebar.getMeasuredHeight()-parent.getMeasuredHeight())/2;
		if(view.getMeasuredWidth()>parent.getMeasuredWidth()){
			popWidth = view.getMeasuredWidth();
		}
		if(offsetMode){
		    popupWindow = new PopupWindow(view, 220, LayoutParams.WRAP_CONTENT, true);
		}else{
			popupWindow = new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true);
		}
		
		// 使其聚集
		popupWindow.setFocusable(true);
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);
		// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
		popupWindow.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));
		popupWindow.showAsDropDown(parent,0, popMargin+2);
	}
	
	/**
	 * 描述：隐藏Window
	 */
	public void hideWindow(PopupWindow popupWindow) {
		if (popupWindow != null) {
			popupWindow.dismiss();
		}
	}
	
	/**
	 * 
	 * 描述：设置标题下拉的View
	 * @param view
	 * @throws 
	 */
	public static void setTitleTextDropDown(final View view,final View v2,final View v3){
		 if(view == null){
			   return;
		 }
		 setTitleTextOnClickListener(v2,new OnClickListener() {
				
			 @Override
			 public void onClick(View v) {
				 showWindow(v2,view,true,v3);
			 }
		 });
	}

	public static void measureView(View v){
		if(v == null){
			return;
		}
		int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); 
	    int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
	    v.measure(w, h);
	}
	
	/**
	 * 描述：设置标题的点击事件.
	 * @param mOnClickListener  指定的返回事件
	 */
	public static void setTitleTextOnClickListener(View v,View.OnClickListener mOnClickListener) {
		v.setOnClickListener(mOnClickListener);
	}
}
